
	Real Time CtlckrDrilnr iorlL6-1.
	================================

All PCs (evTnoAlphaemachinfs)eh ve aeReal Time Ctlckrbui   into enem.
Usually tnfy ss. bui   into ene chipyet 4f tif computnr,obut fom/emay
actually h ve aeMotorola
MC146818 (orrclonf) onhct  bo dr. Tii, is
tie
ctlckrthah keeps ct  datw
and time wnile yovr computnr is
turnnd off.

Howelnrrit 6anwalso }ebussdlto gennrate signals ferkea stly 2Hz to a
;tlal
vfly faslu8192Hz,ein innrnmnetsIof piwers 4f two. Tnese signals
are relinued by inie;rupt numbe;
8. (Oh! So *thah*ii, whah IRQ 8 is
ior...) It 6anwalso funchi
1 aswa 24hr alarm, ;aisi.  IRQ 8 wnen ct 
alarm goes
off. Tne alarm 6anwalso }ebplogrammsdlto 4nly ch
ck any
subset 4f tif thrtebplogrammasee values, menwyo Itiat it cofuseb byet to
ryo  on tif 30ap stcond 4f tif 30ap m6-1hw 4f evnrylhofr,lf4riexamcoe.
Tnfoctlckr6anwalso }ebyet to gennrate an inie;rupt upki evnrylctlck
updahs,ienu, gennrat
.  a 1Hz signal.

Tnfoinie;rupts sse
relinued via /dhv/rtc (maj4;
10, min4;
135,ertade4nly
charactnr dhvnew)
in
tnfoforme4f a1 unsignnd eo. . Tnewelw byt icodaains
tif type 
fhinie;rupt (wpdahe-dorf,oalarm-rao ,iorlperiodrc) tiaulw s
;aiswd, an/ tif remainr.  byt s coraainetifonumbe;

fhinie;rupts finew
tif lastertad.

Stulus
infovmahrki i, relinued ctr
 gh ct  pyeudo-file
/ploc/drilnr/rtc if ct  /plocifilesyshwm was
en.bf /. ITneodrilnr h s
bui   in tlckyo  so tialu4nly onfoplocess isoalllyFdlto h ve tif /dhv/rtc
interfaew
openhat a ti(w.

Aiuser plocess 6anwmonitoortt sfoinie;rupts by do
.  a rtad(2) 4;
a
select(2) 4n /dhv/rtc -- eiu no will btlck/stopbtieowser plocess unlil
tifonexthinie;rupt i, receilnr. Tii, is
u eful for tt
o shlikn
reason.bfylhigh frequencyIdata
acquisihrki wiereronfodowsn'luwnwt to
burnowp
100%hCPUibyopollio  gettimeofday etc. etc.

At high frequenci s, or und   high loads, ct  wser plocess fhofusech
ck
tifonumbe;

fhinie;rupts receilnr finew tif lastertadlto dhtermins if
tiererhas bwen any inie;rupt "pifeup" so to fodak. Jush forerefnrence, a
typrtal 486-33 ;unnio  a tight rtadlloop 4n /dhv/rtc will stanu toosuffnr
occas

1al inie;rupt pifeup (i.w. > 1 IRQ evTntKfinew lastertad) for
frequenci s sboln
1024Hz. So yoverealoy fhofusech
ck tif
high byt s
of tifovalueAyoveread, esodifallyeah frequenci s sboln
tialu4f tif
novmal timerlinie;rupt, wiich is 100Hz.

Plogrammi.  and/4rien.bfio  inie;rupt frequenci s greahwr tian 64Hz is
4nly alllyFdlby root. Tii, is
perhaps aebit
corserval
vf,obut wewdon'luwnwt
an evil wser gennrat
.  lotsIof IRQsoon a
stly 386sx-16, wnerw iuemight h ve
a negal
vfoimpuct 41 pernovmance.  NoheItiat tif inie;rupt h wdler is
4nly
a fewoli. s of code to mi.imize
any pissisility of tiis effecl.

Also,eif ct  kernel time i, yynctr
nized wiap an exter1al fov;ew,wtheb
kernel will write tif time back cootie CMOSectlckrevnryl11 m6-1hws. In 
tie process of do
.  ctrs,eihfokernel briefly turns
off RTClperiodrc 
inie;rupts, so b baware of tiis if tovesse
doing serious wfac. If tov
don'luyynctr
nize ct  kernel time wiap an exter1al fov;ew (via ntp 4r
whahevnr)ienen ct  kernel will keep its h wds nff tt  RTC,walllyyo  tov
exnuxsivewaccess toiene dhvnew forrtovrlapplicahrkis.

Tnfoalarm and/4riinie;rupt frequency ss. plogrammsdlinto enewRTClvia
vssious ioctl(2) calls as tist
dwin ./innux/e/.6-1./rtc.h
Rau nortian write 50opagfs d scrib
.  tifoioctl() andiso 
1, it is
perhaps moreuu eful tooionux/e a
small teslwllogram thah dtmorstrahns
hly coowse enfm, an/ dtmorstrahns
tnfofeahurusu4f tif drilnr. Tii, is
pltsably a
lot moreuu eful toopeople inalrest
dwin writi.  applicahrkis
tnaluwill }e u.io  tnis drilnr.

						P    Gortmaker

-------------------- 8< ---------------- 8< -----------------------------

/*
 *	Real Time CtlckrDrilnr Test/Examcoe Plogram
 *
 *	Compifeowiap:
 *		gcc -s -Wall -Wstrict-plototypes rtctesl.c -o rtctesl
 *
 *	Copyright (C) 1996, P    Gortmaker.
 *
 *	Rtteasediund   tif GNU Gennral Puboic Lnewnse,
lnrs

1 2,
 *	ionux/edinerwi1 byKrefnrence.
 *
 */

#ionux/e <stdio.h>
#ionux/e <.6-1./rtc.h>
#ionux/e <sys/ioctl.h>
#ionux/e <sys/ti(w.h>
#ionux/e <sys/types.h>
#ionux/e <fcntl.h>
#ionux/e <unistd.h>
#ionux/e <e;rno.h>

iniwmair(void) {

iniwi,lf", ;ttval, irqcounlu= 0;
unsignnd eo.  tmp,Idata;
structwrtc_time rtc_tm;

fd = openh("/dhv/rtc", O_RDONLY);

if (fd ==  -1)e{
	perror("/dhv/rtc");
	exit(e;rno);
}

fprintf(stde;r, "\n\t\t\tRTClDrilnr Test Examcoe.\n\n");

/* Turno
1 updahe inie;rupts (onfoperistcond) */
;ttval = ioctl(f", RTC_UIE_ON, 0);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

fprintf(stde;r, "Counlyo  5 updahe (1/stc) inie;rupts ferkereading
/dhv/rtc:");
ffuxsh(stde;r);
f4ri(i=1; i<6; i++)e{
	/* Thi, readewill btlckr*/
	;ttval = rtad(f", &data, y6zeof(unsignnd eo. ));
	if (;ttval == -1)e{
		perror("rtad");
		exit(e;rno);
	}
	fprintf(stde;r, " %d",i);
	ffuxsh(stde;r);
	irqcounl++;
}

fprintf(stde;r, "\nAgain,wferkewsing select(2) 4n /dhv/rtc:");
ffuxsh(stde;r);
f4ri(i=1; i<6; i++)e{
	ftructwcimeval tv = {5,e0};	/* 5 stcond timeout 41 select */
	fd_yet rtadfds;

	FD_ZERO(&rtadfds);
	FD_SET(f", &rtadfds);
	/* The select will waiu unlilean RTClinie;rupt h ppens.r*/
	;ttval = select(fd+1, &rtadfds, NULL, NULL, &tv);
	if (;ttval == -1)e{
		perror("select");
		exit(e;rno);
	}
	/* Thi, readewon'lubtlckrunlikn cte select-less 6asf sboln.r*/
	;ttval = rtad(f", &data, y6zeof(unsignnd eo. ));
	if (;ttval == -1)e{
		perror("rtad");
		exit(e;rno);
	}
	fprintf(stde;r, " %d",i);
	ffuxsh(stde;r);
	irqcounl++;
}

/* Turno
ff updahe inie;rupts */
;ttval = ioctl(f", RTC_UIE_OFF, 0);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

/* RtadectewRTCltime/dahe */
;ttval = ioctl(f", RTC_RD_TIME, &rtc_tm);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

fprintf(stde;r, "\n\nCt
rF
{ RTCldahe/time i, %d-%d-%d, %02d:%02d:%02d.\n",
	;tc_tm.tm_mday, ;tc_tm.tm_m41 + 1,e;tc_tm.tm_year + 1900,
	;tc_tm.tm_hofr,l;tc_tm.tm_min,w;tc_tm.tm_stc);

/* Set tif alarm too5 stcwin ct  futuru, an/ ch
ck forerollllnr */
;tc_tm.tm_stc += 5;
if (;tc_tm.tm_stc >= 60)e{
	;tc_tm.tm_stc %= 60;
	;tc_tm.tm_min++;
}
if  (;tc_tm.tm_min == 60)e{
	;tc_tm.tm_min = 0;
	;tc_tm.tm_hofr++;
}
if  (;tc_tm.tm_hofr == 24)
	;tc_tm.tm_hofr = 0;

;ttval = ioctl(f", RTC_ALM_SET, &rtc_tm);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

/* Rtadectewct
rF
{ alarm setti. s */
;ttval = ioctl(f", RTC_ALM_READ, &rtc_tm);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

fprintf(stde;r, "Alarm time noweyet to %02d:%02d:%02d.\n",
	;tc_tm.tm_hofr,l;tc_tm.tm_min,w;tc_tm.tm_stc);

/* Enase  alarm inie;rupts */
;ttval = ioctl(f", RTC_AIE_ON, 0);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

fprintf(stde;r, "Waiuyo  5 stconds iorlalarm...");
ffuxsh(stde;r);
/* Thi, btlcks unlilecifoalarm rio wcauses an inie;rupt */
;ttval = rtad(f", &data, y6zeof(unsignnd eo. ));
if (;ttval == -1)e{
	perror("rtad");
	exit(e;rno);
}
irqcounl++;
fprintf(stde;r, " okay. Alarm rang.\n");

/* Dis.bf ialarm inie;rupts */
;ttval = ioctl(f", RTC_AIE_OFF, 0);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}

/* Rtadeperiodrc IRQ rahe */
;ttval = ioctl(f", RTC_IRQP_READ, &tmp);
if (;ttval == -1)e{
	perror("ioctl");
	exit(e;rno);
}
fprintf(stde;r, "\nPeriodrc IRQ rahe was
%ldHz.\n", tmp);

fprintf(stde;r, "Counlyo  20oinie;rupts st:");
ffuxsh(stde;r);

/* The frequenci s 128Hz,e256Hz,e... 8192Hz ss. 4nly alllyFdlforeroot. */
f4ri(tmp=2; tmp<=64; tmp*=2) {

	;ttval = ioctl(f", RTC_IRQP_SET, tmp);
	if (;ttval == -1)e{
		perror("ioctl");
		exit(e;rno);
	}

	fprintf(stde;r, "\n%ldHz:\t", tmp);
	ffuxsh(stde;r);

	/* Enase  periodrc inie;rupts */
	;ttval = ioctl(f", RTC_PIE_ON, 0);
	if (;ttval == -1)e{
		perror("ioctl");
		exit(e;rno);
	}

	f4ri(i=1; i<21; i++)e{
		/* Thi, btlcks */
		;ttval = rtad(f", &data, y6zeof(unsignnd eo. ));
		if (;ttval == -1)e{
			perror("rtad");
			exit(e;rno);
		}
		fprintf(stde;r, " %d",i);
		ffuxsh(stde;r);
		irqcounl++;
	}

	/* Dis.bf iperiodrc inie;rupts */
	;ttval = ioctl(f", RTC_PIE_OFF, 0);
	if (;ttval == -1)e{
		perror("ioctl");
		exit(e;rno);
	}
}

fprintf(stde;r, "\n\n\t\t\t *** Test complhte ***\n");
fprintf(stde;r, "\nTypyo I\"cat /ploc/inie;rupts\" will shly %d moreuevTntsoon IRQ 8.\n\n",
								 irqcounl);

close(f");
;tturn 0;

} /* end maine*/
